

<template>
    <tm-app class="groupMembers" bgImg="f5">
        <!-- <image
            class="psa"
            style="width:750rpx;height:834rpx;top:0;left:0;right:0;z-index:1;"
            src="../../static/bg1.jpg"
            mode=""
        ></image> -->
        <view
            style="background: #fff"
            :style="{ height: systemInfo.statusBarHeight + 'px' }"
        ></view>
        <view
            class="slim_header d-flex a-center j-center"
            :style="{ top: systemInfo.statusBarHeight + 'px' }"
        >
            <view class="left_box" @tap="goBack">
                <image src="/static/center/back.png" class="back"></image>
            </view>
            <view class="title d-flex a-center j-between">查找聊天记录</view>
            <view class="right_box">
            </view>
        </view>
        <view
            class="content"
            :style="{ top: `calc(100rpx + ${systemInfo.statusBarHeight}px)` }"
        >
        <view class="search_box d-flex j-center">
                <view class="search d-flex a-center">
                    <i
                        class="tmicon tmicon-search"
                        style="color: #ccc; font-size: 30rpx"
                    ></i>
                    <input
                        @input="search"
                        v-model="searchText"
                        type="text"
                        class="inp"
                        placeholder="成员昵称"
                    />
                </view>
            </view>
        </view>

    </tm-app>
</template>

<script setup lang="ts">
import { ref, reactive,nextTick,watch } from "vue";
import { onReady, onShow, onLoad,onUnload } from "@dcloudio/uni-app";
import { userData as userDataStore } from "@/store/userData";
import { systemInfo as systemInfoStore } from "@/store/systemInfo";
import { groupUpdate} from "@/api/api";
import { getMessageList } from "@/hooks/imHooks";
import { config as configStore } from "@/store/config";


const groupId = ref('')

const goBack = () => {
    uni.navigateBack();
};


const userData: any = userDataStore();

const systemInfo: any = systemInfoStore();

const bool = ref(false);
const updateLoading = ref(false);

const nickName = ref('');

const checkLength = (maxLength:number)=>{
    let content = nickName.value.trim();
    let str = ''
    let len=0;
    for(var i=0; i<content.length; i++) {
        //正则表达式判断中文
        if (/[\u4e00-\u9fa5]/.test(content[i])) {
            len+=2;
        } else {
            len++;
        }

        if(len<=maxLength){
            str+=content[i];
        }
    }
    nextTick(() => {
        nickName.value = str;
    })
}

const conversationID = ref('');
const nextReqMessageID = ref(null);
const isCompleted = ref(false);

const allMsg:any = ref([]);


const test = async()=>{
    console.log('start')
    for(let i=0;i<50000;i++){
        console.log(i);
        const obj = {
            conversationID:conversationID.value,
            nextReqMessageID:null,
        }
        const res:any = await getMessageList(obj);
    }
    console.log('end')
}


const getMsg = async()=>{
    if (!uni.$chat_isSDKReady) {
        setTimeout(() => {
            getMsg();
        }, 200);
        return;
    }

    if(isCompleted.value){
        return;
    }

   const obj = {
    conversationID:conversationID.value,
    nextReqMessageID:nextReqMessageID.value,
   }
   const res:any = await getMessageList(obj);
   const {code,data} = res;
   if(code!==0){
        uni.showToast({
                icon: "none",
                title: '获取聊天记录失败,请稍后再试',
                duration: 1500,
        });
        return;
   }
   allMsg.value = [...data.messageList,...allMsg.value];
   nextReqMessageID.value = data.nextReqMessageID;
   isCompleted.value = data.isCompleted;
   if(!data.isCompleted){
        getMsg();
   }


}

watch(isCompleted,(newVal,oldVal)=>{
    if(newVal){
        console.log('Completed')
        console.log('Completed')
        console.log('Completed')
        console.log('Completed')
        const obj = JSON.parse(JSON.stringify(allMsg.value))
        console.log(obj)
    }
})

//{conversationID: 'C2Ctest',nextReqMessageID}




onReady(() => {});

onLoad((option: any) => {
    if (option.groupID) {
        conversationID.value = `GROUP${option.groupID}`;
        //getMsg();
    }
});

onUnload(() => {});

onShow(() => {});
</script>

<style lang="scss" scoped>
.groupMembers{

    .content{
        position:absolute;
        width:750rpx;
        left:0;
        right:0;
        padding-top:20rpx;
        bottom: constant(safe-area-inset-bottom);
        bottom: env(safe-area-inset-bottom);

        .search_box {
            width: 750rpx;
            background: #fff;
            padding-top: 20rpx;
            padding-bottom: 20rpx;
            margin-bottom: 20rpx;
            .search {
                width: 690rpx;
                height: 60rpx;
                border-radius: 30rpx;
                background: #f5f5f5;
                padding: 0 30rpx;
                .inp {
                    width: 570rpx;
                    margin-left: 20rpx;
                    font-size: 25rpx;
                }
            }
        }
        .notice{
            background:#f5f5f5;
            padding:20rpx;
            border-radius: 20rpx;
        }
    }

}
</style>
